package com.example.team.dao.Imp;

import com.example.team.dao.IRecordDao;
import com.example.team.entity.Record;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Component;

import java.util.List;
/**
 *根据接口操作数据库表的接口规范，对其进行实现
 * 编写者：沈超
 * 时间：2023/11/1
 */
@Component
public class IRecordDaoImpl implements IRecordDao {
    @Autowired
    private JdbcTemplate jdbcTemplate;
    @Override
    public int insertRecord(Record record) {
        return jdbcTemplate.update("insert into record(Id, Name, FromTime) "+
                "value(?,?,?)"
        ,record.getId()
        ,record.getName()
        ,record.getFromTime()
        );
    }

    @Override
    public int deleteRecord(Record record) {
        return jdbcTemplate.update("delete from record "+
                "where Id=?",record.getId());
    }

    @Override
    public List<Record> getAllRecord(Record record) {
        return jdbcTemplate.query("select "+
                "Id,Name,FromTime "+"from record",new Object[]{},
                new BeanPropertyRowMapper<>(Record.class));
    }

    @Override
    public int updateRecord(Record record) {
        return jdbcTemplate.update("update record set "+
                "Name=?,FromTime=? "+
                "where Id=?"
                ,record.getName()
                ,record.getFromTime()
                ,record.getId());
    }

    @Override
    public Record getRecord(Record record) {
        List<Record> result=jdbcTemplate.query("select " +
                        "Id,Name,FromTime " +
                        "from record where Id='"+record.getId()+"'",
                new Object[]{},
                new BeanPropertyRowMapper<>(Record.class));
        if (result.size()>=1)
            return result.get(0);
        else return null;
    }
}
